package com.study.Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 通过实现Callable接口该方式具有返回值
 */
public class ImpCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable=new MyCallable();
        FutureTask<Integer> ft=new FutureTask<Integer>(callable);
        for (int i = 0; i <20 ; i++) {
            System.out.println(Thread.currentThread().getName()+"------->"+i);
            if (i/2==0){
                Thread thread = new Thread(ft);
                thread.start();
            }
        }
        System.out.println("主线程for循环结束------------");
        Integer integer = ft.get();
        System.out.println("返回值是"+integer);

    }
}
class MyCallable implements Callable<Integer>{
    private static int i=0;
    public Integer call() throws Exception {
        int sum=0;
        for (; i < 10; i++) {
            sum=sum+i;
        }
        System.out.println(Thread.currentThread().getName()+"返回值为--->"+sum);
        return sum;
    }
}
